/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package chameleon.model.game.transition;

import chameleon.model.game.Tag;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * This class represent the description of a transition relation. They contains
 * a goal file that represents an automata.
 *
 * @author simon
 */
public class Automata extends TransitionRelationDescription {

    public Automata(File automata, int player ){
        super(Tag.AUTOMATA);
        representation_ = automata;
        player_ = player;
    }

    /**
     * This method convert the automata to a Nondeterministic Büchi Word (NBW)
     * via GOAL command.
     */

    @Override
    public void convert2NBW() {
        FileWriter fw = null;
        try {
            String path = representation_.getPath();
            char[] pathChar = path.toCharArray();
            File script = new File("script/convert2NBW");
            fw = new FileWriter(script);
            fw.write("#!/bin/bash\n");
            if (pathChar[0] == '/') {
                fw.write("GOAL-20110214/goal convert -t ngbw -o ../GoalFile/ngbw.gff " + path + "\n");
            } else {
                fw.write("GOAL-20110214/goal convert -t ngbw -o ../GoalFile/ngbw.gff ../" + path + "\n");
            }
            fw.write("GOAL-20110214/goal convert -t nbw -o ../GoalFile/nbw"+player_+".gff " + "../GoalFile/ngbw.gff\n");
            //fw.write("GOAL-20110214/goal simplify  -o ../GoalFile/nbw"+player_+".gff ../GoalFile/nbw"+player_+".gff");
            fw.close();
            //CALL GOAL
            Runtime runtime = Runtime.getRuntime();
            runtime.exec("chmod u+x script/convert2NBW");
            Process p = runtime.exec(new String[]{"script/convert2NBW"});
            try {
                p.waitFor();
            } catch (InterruptedException ex) {
                Logger.getLogger(Automata.class.getName()).log(Level.SEVERE, null, ex);
            }
            script.delete();
            representation_ = new File("GoalFile/nbw"+player_+".gff");
        } catch (IOException ex) {
            Logger.getLogger(Automata.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fw.close();
            } catch (IOException ex) {
                Logger.getLogger(Automata.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        File ngbw = new File ("GoalFile/ngbw.gff");
        ngbw.delete();
        isNBW_=true;
    }

    
}
